home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 August
/
Macworld (1999-08).dmg
/
Shareware World
/
Info
/
For Developers
/
MADE 1.4.0
/
Essentials
/
Essential Memory.c
< prev
next >
Wrap
Text File
|
1999-05-26
|
6KB
|
238 lines
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* MADE - Macintosh Application Development Essentials */
/* --------------------------------------------------- */
/* (c) Sig Software, http://www.sigsoftware.com/ */
/* */
/* These files can only be used for experimental purposes. To obtain */
/* fully commented code, source code for the functions in Essential */
/* Extras.h and permission for usage in final projects, you must */
/* purchase a license. See documentation for more information. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* Essential Memory.c */
/* ------------------ */
/* */
/* Memory allocation, deallocation and initialisation. */
/* */
/* Version 1.0.0 - 10th November 1996 */
/* Version 1.0.1 - 4th June 1997 - Fixed macro, function names */
/* Version 1.1.0 - 29th January 1998 - New OS function names */
/* Version 1.1.1 - 19th June 1998 - Casts for InitialiseMemory */
/* Version 1.2.0 - 20th November 1998 - Avoids C/C++ warnings */
/* Version 1.4.0 - 26th May 1999 - Added linked list routines */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "Essential Headers.h"
#include "Essential Prototypes.h"
void AddToLinkedList(void** firstPointer, void* addItem, void* beforeItem)
{
void **slotPointer;
slotPointer=firstPointer;
while (1) {
if (*slotPointer==beforeItem) {
*(void**)addItem=beforeItem;
*slotPointer=addItem;
break;
}
Assert(*slotPointer);
if (*slotPointer==0)
break;
slotPointer=(void**)*slotPointer;
}
}
void RemoveFromLinkedList(void** firstPointer, void* removeItem)
{
void **slotPointer;
slotPointer=firstPointer;
while (1) {
Assert(*slotPointer);
if (*slotPointer==0)
break;
if (*slotPointer==removeItem) {
*slotPointer=*(void**)removeItem;
break;
}
slotPointer=(void**)*slotPointer;
}
}
void* GetLinkedListItem(void* firstPointer, long itemNumber)
{
void *itemPointer;
itemPointer=firstPointer;
while(itemNumber) {
itemPointer=*(void**)itemPointer;
Assert(itemPointer);
if (itemPointer==0)
break;
itemNumber--;
}
return itemPointer;
}
long CountLinkedListItems(void* firstPointer)
{
void *itemPointer;
long count;
count=0;
itemPointer=firstPointer;
while (itemPointer) {
count++;
itemPointer=*(void**)itemPointer;
}
return count;
}
Error CreateHeapSpace(Size required)
{
Error error=0;
Size allocate;
allocate=required+Emergency_Memory_Reserve;
if (MaxBlock()<allocate) {
PurgeMem(allocate);
if (MaxBlock()<allocate) {
MyFreeUpMemory(allocate);
if (MaxBlock()<allocate)
error=memFullErr;
}
}
TestError(error);
return error;
}
void* AllocPtr(Error* error, Size allocate)
{
void* pointer;
*error=CreateHeapSpace(allocate);
_i(*error)
pointer=(void*)NewPtr(allocate);
*error=TestMemError(pointer);
_i(*error);
#if Project_Under_Development && Initialise_Allocated_Memory
InitialiseMemory((char*)pointer, allocate);
#endif
return pointer;
_e
return 0;
}
void** AllocHandle(Error* error, Size allocate)
{
void** handle;
*error=CreateHeapSpace(allocate);
_i(*error)
handle=(void**)NewHandle(allocate);
*error=TestMemError(handle);
_i(*error);
#if Project_Under_Development
#if Move_On_Handle_Allocation
MoveHHi((Handle)handle);
#endif
#if Initialise_Allocated_Memory
InitialiseMemory((char*)*handle, allocate);
#endif
#endif
return handle;
_e
return 0;
}
void DestroyPtr(void* pointer)
{
#if Project_Under_Development && Initialise_Allocated_Memory
InitialiseMemory((char*)pointer, GetPtrSize((Ptr)pointer));
#endif
DisposePtr((Ptr)pointer);
}
void DestroyHandle(void** handle)
{
#if Project_Under_Development && Initialise_Allocated_Memory
InitialiseMemory((char*)*handle, GetHandleSize((Handle)handle));
#endif
DisposeHandle((Handle)handle);
}
#if Project_Under_Development
#if Assert_Memory_Locking
// I'm sure the next two routines could be written better with more knowledge of
// Apple's System Software, but the programming guidelines don't offer such
// information, so it's likely to change. Thus I do this the long way round.
void LockHandleAssert(void** handle)
{
SignedByte statusBefore, statusAfter;
statusBefore=HGetState((Handle)handle);
HLock((Handle)handle);
statusAfter=HGetState((Handle)handle);
Assert(statusBefore!=statusAfter);
}
void UnlockHandleAssert(void** handle)
{
SignedByte statusBefore, statusAfter;
statusBefore=HGetState((Handle)handle);
HUnlock((Handle)handle);
statusAfter=HGetState((Handle)handle);
Assert(statusBefore!=statusAfter);
}
#endif
#if Initialise_Allocated_Memory
// This could be optimised by first filling with long words of 0xAAAAAAAA, but there's
// little point as it's only used when the project is under development anyway.
void InitialiseMemory(char* pointer, Size length)
{
char *trasher, *topAddress;
trasher=pointer;
topAddress=trasher+length;
while (trasher<topAddress)
*trasher++=(char)0xAA;
}
#endif
#endif